package com.jixiantubu.jinnang;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.jixiantubu.adapter.YouJiAdapter;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.denglu.Login;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class JinNang_YouJiActivity extends Activity implements OnClickListener {
	@ViewInject(R.id.JinNang_YouJi_lv)
	private PullToRefreshListView listView;
	ImageView gobackImageView, writeImageView, findImageView;
	private YouJiAdapter adapter;
	List<Travelsbean> list = new ArrayList<Travelsbean>();
	private BitmapUtils bitmapUtils;
	private String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/KePuTuiJian_Middle";
	private int number = 1;// 默认第一页
	Travelsbean travelsbean = new Travelsbean();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.jinnang_youji);

		ViewUtils.inject(this);
		bitmapUtils = new BitmapUtils(this);
		initViews();
		setListView();
		initData(url);

	}

	private void setListView() {
		// 设置PullToRefreshListView添加上拉和下拉的方式
		listView.setMode(Mode.BOTH);// 现在是两种都用的情况

		// 在布局文件中通过添加属性(ptr:ptrAnimationStyle="rotate")控制加载图标的动画效果(旋转[rotate]
		// 翻转[flip])

		// 修改刷新布局的参数
		/*
		 * includeStart = true, includeEnd = false;表示获取下拉刷新的布局
		 * 
		 * includeStart = false, includeEnd = true;表示获取上拉刷新的布局
		 */
		ILoadingLayout freshLayout = listView
				.getLoadingLayoutProxy(true, false);

		// 修改更新的时间，可以用于提示上次刷新是什么时候
		freshLayout.setLastUpdatedLabel("上次更新2015.10.20");
		// 修改更新的旋转图标
		// freshLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));
		// 设置下拉的文字
		freshLayout.setPullLabel("下拉刷新");
		// 松开的提示语
		freshLayout.setReleaseLabel("松开刷新");
		// 设置刷新的文字提示
		freshLayout.setRefreshingLabel("正在刷新!请稍等");
		// 设置字体格式
		// freshLayout.setTextTypeface(Typeface.DEFAULT);

		/**
		 * 给PullToRefreshListView添加headView
		 */
		ListView list1 = listView.getRefreshableView();// 获取PullToRefreshListView中的ListView
		ImageView img = new ImageView(this);
		// img.setImageResource(R.drawable.wo);
		list1.addHeaderView(img);

		/**
		 * 为PullToRefreshListView绑定监听器监听用户的下拉和上拉操作
		 */
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			// 下拉刷新
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// 下拉刷新的逻辑代码
				/*
				 * 1、清空数据集合中的原有的所有的数据
				 * 
				 * 2、重新请求第一页数据，存入到数据集合中
				 * 
				 * 3、通知UI展示新数据(notifyDataSetChanged)
				 * 
				 * 4、通知刷新布局，数据加载完成
				 */

				// 1、清空数据集合中的原有的所有的数据
				list.clear();
				// 2、重新请求第一页数据，存入到数据集合中 3、通知UI展示新数据(notifyDataSetChanged)
				initData(url);
				// 4、通知刷新布局，数据加载完成
				listView.onRefreshComplete();
			}

			// int number = (Integer)(adapter.getCount()/5) ;
			protected void addMoreData() {
				// 刷新之后显示下一页内容，当亲为第一页
				number++;// 页数自加；
				if (0 != adapter.getCount()%5) {
					Toast.makeText(JinNang_YouJiActivity.this, "已全部显示",
							Toast.LENGTH_SHORT).show();
				} else {
					String url = "http://" + MyApplication.getIP()
							+ ":8080/Jinnang/KePuTuiJian_Middle?key=2&page="
							+ number;
					// list.add(travelsbean);
					initData(url);

				}
			}

			@Override
			// 上拉加载
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// 上拉加载的逻辑代码
				/*
				 * 1、请求下一页数据
				 * 
				 * 2、添加到数据集合中
				 * 
				 * 3、通知UI展示新数据(notifyDataSetChanged)
				 * 
				 * 4、通知刷新布局，数据加载完成
				 */
				addMoreData();
				/*
				 * // 1、请求下一页数据 page++; // 2、添加到数据集合中
				 * 3、通知UI展示新数据(notifyDataSetChanged) initData(page);
				 */
				// 4、通知刷新布局，数据加载完成
				listView.onRefreshComplete();

			}
		});

	}

	private void initData(String url) {
		final CustomProgressDialog pd = new CustomProgressDialog(
		JinNang_YouJiActivity.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		// 绑定参数
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("key", "5");
		params.addQueryStringParameter("page", "1");
		HttpUtils httpUtils = new HttpUtils();
		MyApplication.httpclient.configSoTimeout(5000);
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						MyApplication.show(JinNang_YouJiActivity.this, "加载数据失败，请检查网络!");
						pd.dismiss();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						pd.dismiss();
						// 请求成功
						String result = arg0.result;
						Log.e("!!!!", result.toString());
						Gson gson = new Gson();
						Type type = new TypeToken<List<Travelsbean>>() {
						}.getType();
						List<Travelsbean> list1 = gson.fromJson(result, type);
						list.addAll(list1);
						adapter.setList(list);
						Log.e("!胡蛟龙!", list.toString());
						adapter.notifyDataSetChanged();
					}
				});
	}

	private void initViews() {

		// 初始化适配器
		adapter = new YouJiAdapter(JinNang_YouJiActivity.this, list);
		// 绑定适配器
		listView.setAdapter(adapter);
		gobackImageView = (ImageView) findViewById(R.id.JinNang_youji_goback);
		writeImageView = (ImageView) findViewById(R.id.JinNang_youji_write);
		findImageView = (ImageView) findViewById(R.id.JinNang_youji_find);
		gobackImageView.setOnClickListener(this);
		writeImageView.setOnClickListener(this);
		findImageView.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.JinNang_youji_goback:
			finish();
			break;
		case R.id.JinNang_youji_write:
			if (!(MyApplication.user_id==null)) {
				Intent intent = new Intent(JinNang_YouJiActivity.this,
						JinNang_YouJi_Write.class);
				startActivity(intent);
			}else {
				MyApplication.show(JinNang_YouJiActivity.this, "对不起！您没有登录账号，无法发表游记");
			}
			
			break;
		case R.id.JinNang_youji_find:
			Intent intent1 = new Intent(JinNang_YouJiActivity.this,
					JinNang_YouJi_Find.class);
			startActivity(intent1);
			break;
		default:
			break;
		}

	}
}
